<?php
namespace app\banquet\model;

use app\BaseModel;
use think\facade\Db;

class Package extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_banquet_package';


    protected $append = [

        'package_goods'
    ];


    /**
     * @author chenniang
     * @DataTime: 2021-12-27 11:56
     * @功能说明:
     */
    public function getPackageGoodsAttr($value,$data){

        if(!empty($data['id'])){

            $p_goods_model = new PackageGoods();

            $dis = [

                'package_id' => $data['id']
            ];

            $list = $p_goods_model->where($dis)->select()->toArray();

            return $list;

        }


    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        if(isset($data['package_goods'])){

            $package_goods = $data['package_goods'];

            unset($data['package_goods']);
        }

        $res = $this->insert($data);

        $id  = $this->getLastInsID();

        if(!empty($package_goods)){

            $this->updateSome($package_goods,$id,$data['uniacid']);
        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-27 11:33
     * @功能说明:
     */
    public function updateSome($goods,$id,$uniacid){

        $p_goods_model = new PackageGoods();

        $p_goods_model->where(['package_id'=>$id])->delete();

        if(!empty($goods)){

            foreach ($goods as $k=>$v){

                $v['uniacid'] = $uniacid;

                $v['package_id'] = $id;

                $p_goods_model->insert($v);
            }

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(isset($data['package_goods'])){

            $package_goods = $data['package_goods'];

            unset($data['package_goods']);
        }

        $res = $this->where($dis)->update($data);

        if(!empty($package_goods)){

            $this->updateSome($package_goods,$dis['id'],$data['uniacid']);
        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @param $dis
     * @param int $page
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-01-19 13:45
     */
    public function adminList($dis,$page=10){

        $data = $this->alias('a')
                ->join('longbing_card_banquet_place_package b','a.id = b.package_id','left')
                ->join('longbing_card_banquet_place c','b.place_id = c.id','left')
                ->where($dis)
                ->field('a.*')
                ->group('a.id')
                ->order('a.top desc,a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-12-24 14:55
     * @功能说明:首页的宴席列表
     */
    public function indexSelect($place_id,$page=4){

        $dis[] = ['b.place_id','in',$place_id];

        $dis[] = ['a.status','=',1];

        $data = $this->alias('a')
                ->join('longbing_card_banquet_place_package b','b.package_id = a.id')
                ->where($dis)
                ->field('a.*')
                ->group('a.id')
                ->order('a.top desc,a.id desc')
                ->limit($page)
                ->select()
                ->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-24 14:55
     * @功能说明:首页的宴席列表
     */
    public function indexList($place_id){

        $dis[] = ['b.place_id','in',$place_id];

        $dis[] = ['a.status','=',1];

        $data = $this->alias('a')
            ->join('longbing_card_banquet_place_package b','b.package_id = a.id')
            ->where($dis)
            ->field('a.*')
            ->order('a.top desc,a.id desc')
            ->paginate(10)
            ->toArray();

        return $data;

    }





}